package com.lyhengtongwl.zqsnews.utils;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;

import com.lyhengtongwl.zqsnews.ui.activity.MainActivity;

import java.util.List;

public class NewsForwardAnimation{
    @SuppressLint("StaticFieldLeak")
    public static NewsForwardAnimation instance;
    private Context mContext;
    private List<View> views;
    private View publish;

    private NewsForwardAnimation(Context context, List<View> views, View publish) {
        this.mContext = context;
        this.views = views;
        this.publish = publish;
    }


    public static NewsForwardAnimation getInstance(Context context, List<View> views, View publish) {
        if (instance == null) {
            synchronized (AppConfig.class) {
                if (instance == null) {
                    instance = new NewsForwardAnimation(context, views, publish);
                }
            }
        }
        return instance;
    }

    //打开扇形菜单的属性动画， dp为半径长度
    public void showOpenAnim(int dp) {
        double a;
        double b;
        double x;
        double y;
        views.get(0).setVisibility(View.VISIBLE);
        views.get(1).setVisibility(View.VISIBLE);
        views.get(2).setVisibility(View.VISIBLE);

        //for循环来开始小图标的出现动画
        for (int i = 0; i < views.size(); i++) {
            AnimatorSet set = new AnimatorSet();
            if (i == 0) {
                //标题1与x轴负方向角度为20°，标题2为100°，转换为弧度
                a = Math.cos(-90 * Math.PI / 180 * (i * 2 + 1));
                b = Math.sin(-90 * Math.PI / 180 * (i * 2 + 1));
                x = a * dip2px(dp);
                y = b * dip2px(dp);
            } else if (i == 1) {
                //标题1与x轴负方向角度为20°，标题2为100°，转换为弧度
                a = Math.cos(-15 * Math.PI / 180 * (i * 2 + 1));
                b = Math.sin(-15 * Math.PI / 180 * (i * 2 + 1));
                x = a * dip2px(dp);
                y = b * dip2px(dp);
            } else {
                //标题1与x轴负方向角度为20°，标题2为100°，转换为弧度
                a = Math.cos(0 * Math.PI / -90 * (i * 2 + 1));
                b = Math.sin(0 * Math.PI / -90 * (i * 2 + 1));
                x = a * dip2px(dp);
                y = b * dip2px(dp);
            }


            set.playTogether(
                    ObjectAnimator.ofFloat(views.get(i), "translationX", (float) (x * 0.25), (float) x),
                    ObjectAnimator.ofFloat(views.get(i), "translationY", (float) (y * 0.25), (float) y)
                    , ObjectAnimator.ofFloat(views.get(i), "alpha", 0, 1).setDuration(0)
            );
//            set.setInterpolator(new BounceInterpolator());
            set.setDuration(300);
            set.start();

            set.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    MainActivity.isMenuOpen = true;
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
        }

//        //转动加号大图标本身45°
//        ObjectAnimator rotate = ObjectAnimator.ofFloat(publish, "rotation", 0, 90).setDuration(300);
//        rotate.setInterpolator(new BounceInterpolator());
//        rotate.start();

    }

    //关闭扇形菜单的属性动画，参数与打开时相反
    public void showCloseAnim(int dp) {


        //for循环来开始小图标的出现动画
        for (int i = 0; i < views.size(); i++) {
            AnimatorSet set = new AnimatorSet();
            double a;
            double b;
            double x;
            double y;
            if (i == 0) {
                //标题1与x轴负方向角度为20°，标题2为100°，转换为弧度
                a = Math.cos(-90 * Math.PI / 180 * (i * 2 + 1));
                b = Math.sin(-90 * Math.PI / 180 * (i * 2 + 1));
                x = a * dip2px(dp);
                y = b * dip2px(dp);
            } else if (i == 1) {
                //标题1与x轴负方向角度为20°，标题2为100°，转换为弧度
                a = Math.cos(-15 * Math.PI / 180 * (i * 2 + 1));
                b = Math.sin(-15 * Math.PI / 180 * (i * 2 + 1));
                x = a * dip2px(dp);
                y = b * dip2px(dp);
            } else {
                //标题1与x轴负方向角度为20°，标题2为100°，转换为弧度
                a = Math.cos(0 * Math.PI / -90 * (i * 2 + 1));
                b = Math.sin(0 * Math.PI / -90 * (i * 2 + 1));
                x = a * dip2px(dp);
                y = b * dip2px(dp);
            }


            set.playTogether(
                    ObjectAnimator.ofFloat(views.get(i), "translationX", (float) x, (float) (x * 0.25)),
                    ObjectAnimator.ofFloat(views.get(i), "translationY", (float) y, (float) (y * 0.25)),
                    ObjectAnimator.ofFloat(views.get(i), "alpha", 1, 0).setDuration(0)
            );
//
//            set.setInterpolator(new AccelerateInterpolator());
            set.setDuration(300);
            set.start();

            set.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {

                    views.get(0).setVisibility(View.GONE);
                    views.get(1).setVisibility(View.GONE);
                    views.get(2).setVisibility(View.GONE);
                    MainActivity.isMenuOpen = false;
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
        }


//        //转动加号大图标本身45°
//        ObjectAnimator rotate = ObjectAnimator.ofFloat(publish, "rotation", 90, 0).setDuration(300);
//        rotate.setInterpolator(new BounceInterpolator());
//        rotate.start();


    }

    private int dip2px(int value) {
        float density = mContext.getResources()
                .getDisplayMetrics().density;
        return (int) (density * value + 0.5f);
    }

}
